package com.mmall.util;


import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;


import java.util.Date;

public class DateTimeUtil {

    public  static String FORMATETIME="yyyy-MM-dd HH:mm:ss";

    public static Date strToDate(String dateStr, String formatStr) {

        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateStr);
        return dateTime.toDate();
    }

    public static String DatetoStr(Date date, String formatStr) {
        if (date == null) {
            return StringUtils.EMPTY;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(formatStr);
    }
    public static Date strToDate(String dateStr) {

        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(FORMATETIME);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateStr);
        return dateTime.toDate();
    }

    public static String DatetoStr(Date date) {
        if (date == null) {
            return StringUtils.EMPTY;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(FORMATETIME);
    }

    public static void main(String[] args) {

        System.out.println(DateTimeUtil.DatetoStr(new Date()));

        System.out.println(DateTimeUtil.strToDate("2017-09-24 20:01:01"));
    }


}
